package com.rdc.qingkou.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.MessageRecyclerViewAdapter;
import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.contract.MessageContract;
import com.rdc.qingkou.entity.MessageBean;
import com.rdc.qingkou.entity.MessageNewsNumResult;
import com.rdc.qingkou.entity.MessageResult;
import com.rdc.qingkou.presenter.MessagePresenter;

import java.util.ArrayList;
import java.util.List;

public class MessageActivity extends AppCompatActivity implements MessageContract.IMessageView {

    private RecyclerView mMessageRecyclerView;
    private ImageButton mBackImageButton;
    private TextView mTitleTextView;
    private SwipeRefreshLayout mSwipeRefresh;
    private MessageRecyclerViewAdapter mMessageRecyclerViewAdapter;
    private List<MessageBean> mMessageList;

    private MessagePresenter mMessagePresenter;

    private Integer hasMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.White), true);
        setContentView(R.layout.activity_message);
//        initData();
//        initView();
        mMessageList = new ArrayList<>();
        mMessagePresenter = new MessagePresenter(this);
        mMessagePresenter.getUnReadNewsNum(MyApplication.getUser().getUserId());

        mMessagePresenter.getMessage(MyApplication.getUser().getUserId(), 0);
//        mMessagePresenter.loadMoreMessage(MyApplication.getUser().getUserId(),0);  //test

        initView();
    }



    private void initData(MessageResult messageResult) {

        List<MessageBean> messageBeanList = new ArrayList<>();
        if (messageResult.getMessage().size() != 0){
            hasMessage = 0;

            Log.d("MeSubscribeFragment" ,"showData");

            for (int i = 0; i < messageResult.getMessage().size(); i++) {
                MessageBean messageBean = new MessageBean();

                messageBean.setUserId(messageResult.getMessage().get(i).getMessages().getUser().getUserId());
                messageBean.setContent(messageResult.getMessage().get(i).getMessages().getContent());
                messageBean.setName(messageResult.getMessage().get(i).getMessages().getUser().getNickName());

                messageBean.setHeadSculptureId(URL.HEAD_YUN2+messageResult.getMessage().get(i).getMessages().getUser().getImageUrl()+"");
                messageBean.setTime(messageResult.getMessage().get(i).getMessages().getDate());
                messageBeanList.add(messageBean);
            }
            mMessageList.clear();
            mMessageList.addAll(messageBeanList);


            mMessageRecyclerViewAdapter.notifyDataSetChanged();
        }else {
            hasMessage = 1;

            mMessageRecyclerView.setBackground(getCurContext().getResources().getDrawable(R.drawable.message_none_message));

        }
    }

    private void initView() {
        mMessageRecyclerView = (RecyclerView) findViewById(R.id.message_recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MessageActivity.this, LinearLayoutManager.VERTICAL, false);
        mMessageRecyclerView.setLayoutManager(layoutManager);
        mMessageRecyclerViewAdapter = new MessageRecyclerViewAdapter(MessageActivity.this, mMessageList);
        mMessageRecyclerViewAdapter.setOnItemClickListener(new MessageRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                //Toast.makeText(MessageActivity.this, "你点击了第" + position, Toast.LENGTH_SHORT).show();
            }
        });
        mMessageRecyclerView.setAdapter(mMessageRecyclerViewAdapter);

        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.message_swipeRefreshLayout);
        mSwipeRefresh.setColorSchemeResources(R.color.colorStart);
        mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        MessageActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                mMessagePresenter.getMessage(MyApplication.getUser().getUserId(), 0);
                                mMessageRecyclerViewAdapter.notifyDataSetChanged();
                                mSwipeRefresh.setRefreshing(false);

                            }
                        });
                    }
                }).start();
            }
        });

        mBackImageButton = (ImageButton) findViewById(R.id.title_back_imageButton);
        mBackImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        mTitleTextView = (TextView) findViewById(R.id.title_content_textView);
        mTitleTextView.setText("消息");
    }

    @Override
    public void showBottom(int lastIndex) {

    }

    @Override
    public Context getCurContext() {
        return this;
        //return null;
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showData(MessageResult messageResult) {
        Log.d("MessageActivity", "MESSAGE RESULT IS : " + messageResult.toString());

        initData(messageResult);
    }

    @Override
    public void showInfo(String info) {
        Toast.makeText(this, "ERROR : " + info, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showUnReadNum(MessageNewsNumResult messageNewsNumResult) {
        Log.d("MessageActivity", "UNREAD NUMBER IS : " + messageNewsNumResult.getMessage());
    }

    @Override
    public void showMoreMessage(MessageResult messageResult) {

        //initData(messageResult);
    }

    @Override
    public void updateNews() {

    }
}
